# coding: utf-8
import sys
import subprocess
from PySide import QtGui
from PySide import QtCore


class Run(QtCore.QThread):
    signal = QtCore.Signal(str)

    def __init__(self):
        super(Run, self).__init__()
        self.input_text = None

    def start_run(self, input_text):
        self.input_text = input_text
        self.start()

    def run(self):
        list_out = self.filt(self.input_text)
        self.signal.emit(list_out)

    @staticmethod
    def filt(s):
        cmd = '{0} {1}'.format('c++filt.exe', ' '.join(map(lambda x: x.strip(), s.split('\n'))))
        return subprocess.check_output(cmd)


class Demo(QtGui.QWidget):
    def __init__(self):
        super(Demo, self).__init__()

        self.input_area = QtGui.QTextEdit()
        self.input_area.setPlainText('_ZNK4Json5ValueixEPKc')
        self.change = QtGui.QPushButton('->')
        self.change.clicked.connect(self.demangling)
        self.output_area = QtGui.QTextEdit()

        layout = QtGui.QHBoxLayout()
        layout.addWidget(self.input_area)
        layout.addWidget(self.change)
        layout.addWidget(self.output_area)
        self.setLayout(layout)

        self.thread = Run()
        self.thread.signal.connect(self.callback)

    def callback(self, output_text):
        self.output_area.setPlainText(output_text)

    def demangling(self):
        self.output_area.clear()
        input_text = self.input_area.toPlainText()
        self.thread.start_run(input_text)


if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    demo = Demo()
    demo.show()
    sys.exit(app.exec_())
